<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #app {
      width: 400px;
      height: 400px;
      background-color: red;
    }
  </style>
</head>

<body>
  <div id="app"></div>
  <script>
    const app = document.getElementById("app")
    // let lastTime = 0
    // 获取两次移动的间隔时间
    // app.onmousemove = function () {
    // const nowTime = Date.now()
    // console.log(nowTime - lastTime);
    // lastTime = nowTime
    // }

    // 基础节流
    // let lastTime = 0
    // app.onmousemove = function () {
    // const nowTime = Date.now()
    // if (nowTime - lastTime < 100) {
    //   return
    // }
    // console.log("核心代码");
    // lastTime = nowTime
    // }

    // 封装节流函数
    function throttle(fn, time) {
      let lastTime = 0
      return function(e){
        const nowTime = Date.now()
        if (nowTime - lastTime < time) {
          return
        }
        lastTime = nowTime

        fn.call(this,e)
      }
    }

    function center(e) {
      console.log(1);
      console.log(this)
      console.log(e);
    }

    app.onmousemove = throttle(center, 100)
  </script>
</body>

</html>